<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <!--      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>-->
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--用户列表卡片-->
    <el-card class="box-card">
      <el-form :inline="true" :model="dept" class="demo-form-inline" label-width="70px">
        <el-form-item label="部门">
          <el-select
            v-model="role.deptId"
            placeholder="请选择所属部门"
          >
            <el-option
              v-for="department in departments"
              :label="department.name"
              :key="department.id"
              :value="department.id"
            >
              <span style="float: left">{{ department.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">
                <el-tag size="small" effect="plain" type="success">
                  {{ department.total }}人
                </el-tag>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" label-width="70px">
          <el-input clearable v-model="role.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 30px">
          <el-button icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" icon="el-icon-search">查询</el-button>
          <el-button type="success" icon="el-icon-plus">添加</el-button>
        </el-form-item>
      </el-form>
      <!--      表格内容显示区域-->
      <el-table
        v-loading="loading"
        :data="roleList"
        border
        max-height="600"
        style="width: 74%;">
        <el-table-column
          prop="id"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色"
          width="110">
        </el-table-column>
        <el-table-column
          prop="deptName"
          label="所属部门"
          width="110">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          width="350">
        </el-table-column>
        <el-table-column
          label="操作">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-tooltip class="item" effect="light" content="授权" placement="top-start">
            <el-button type="info" size="mini" icon="el-icon-s-tools"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" content="菜单" placement="top-start">
            <el-button type="warning" size="mini" icon="el-icon-menu"></el-button>
          </el-tooltip>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top:15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getRoleList } from '../../api/role'
import { getDept } from '../../api/department'

export default {
  name: 'Role',
  data () {
    return {
      //当前页
      current: 1,
      size: 6,
      total: 100,
      loading: true,
      role: {
        name: '',
        deptId: ''

      },
      roleList: [],
      departments: [],
      // 角色对话框的可见性
    }
  },
  created () {
    this.getRoleList()
    //创建组件的时候调用查询所有用户的方法
    this.getDepartments()
    setTimeout(() => {
      this.loading = false
    }, 500)
  },
  methods: {
    async getDepartments () {
      const res = await getDept()
      if (res.code != 200) {
        return this.$message.error('获取部门列表失败:' + res.message)
      }
      this.departments = res.data.dept
    },
    // 每页条数改变
    handleSizeChange (val) {
      this.size = val
      this.getDept()
      // console.log(`每页 ${val} 条`);
    },
    // 当前页码改变
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.current = val
      this.getDept()
    },
    async getRoleList () {

      const res = await getRoleList(this.current, this.size, this.role)
      if (res.code !== 200) {
        return this.$message.error('获取角色列表失败:' + res.message)
      }
      this.roleList = res.data.role
      this.total = res.data.total
    },
  }
}
</script>

<style scoped>

</style>
